package cn.wolfcode.luowowo.acticle.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.Region;
import cn.wolfcode.luowowo.acticle.domain.Strategy;
import cn.wolfcode.luowowo.acticle.mapper.DestinationMapper;
import cn.wolfcode.luowowo.acticle.mapper.RegionMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyMapper;
import cn.wolfcode.luowowo.acticle.query.RegionQuery;
import cn.wolfcode.luowowo.acticle.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RegionServiceImpl  implements IRegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public PageInfo query(RegionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(regionMapper.selectForList(qo));
    }

    @Override
    public List<Region> queryByParentId(Long pid) {
        return regionMapper.selectByParentId(pid);
    }

    @Override
    public void saveOrUpdate(Region region) {
        if(region.getId() == null){
            regionMapper.insert(region);
        }else{
            regionMapper.updateByPrimaryKey(region);
        }
    }

    @Override
    public void delete(Long id) {
        regionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void changeHotValue(Long id, boolean hot) {
        regionMapper.updateHotValue(id, hot);
    }

    @Override
    public List<Destination> getRefDestination(Long rid) {
       /* Region region = regionMapper.selectByPrimaryKey(rid);
        if (Region.TYPE_AREA.equals(region.getType())){
            //区域：按照区域查
            return destinationMapper.selectDestinationByRegionIds(region.getRefIds());
        }else if(Region.TYPE_CITY.equals(region.getType())){
            //城市：按照id查询
            return destinationMapper.selectDestinationByIds(region.getRefIds());
        }else if (Region.TYPE_COUNTRY.equals(region.getType())){
            //国家：按照父id查
            return destinationMapper.selectDestinationByParentIds(region.getRefIds());
        }*/

        return Collections.emptyList();
    }

    @Override
    public Region get(Long id) {
        return regionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> getRefDestinationByRegionType(String type) {
        return destinationMapper.selectDestinationByType(type);
    }

    @Override
    public List<Map<String, Object>> getDestByDeep(int deep) {
        return destinationMapper.selectDestinationByDeep(deep);
    }

    @Override
    public List<Map<String, Object>> getRefDestinationByStrategyId(Long strategyId) {
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyId);
        return destinationMapper.selectRefDestinationAndSubDestByParendId(strategy.getDest().getId());
    }


    @Override
    public List<Region> getHotRegions() {
        return regionMapper.selectHotRegions();
    }

    @Override
    public List<Destination> getHotDestination(Long regionId) {
        if(regionId == -1){
            return destinationMapper.selectDestinationByParentIds(new Long[]{1L});
        }
        Region region = regionMapper.selectByPrimaryKey(regionId);
        return destinationMapper.selectHotDestinationByIds(region.getRefIds());
    }
}
